﻿using System;
using LX.Core.Validation;
using S4Store.Domain;
using S4Store.Persistent;
using System.Linq;
using S4Store.Domain.NetMarketingKpi;

namespace S4Store.Service.Valid.NetMarketingMediaValid
{
    public class Add : ValidExpression<NetMarketingMedia>
    {
        public S4StoreDataContext PersistentContext { get; set; }

        public override void ValidEntity(ValidationResult result, NetMarketingMedia entity)
        {
            NoEmpty(result, "名称", entity.Name);
            if (entity.StoreId ==0)
            {
                PushError(result, "请选择所属店");
            }
            if (entity.TypeValue == 0)
            {
                PushError(result, "请选择类别");
            }
            if (PersistentContext.Set<NetMarketingMedia>().AsQueryable().FirstOrDefault(t => t.Name == entity.Name && t.StoreId == entity.StoreId) != null)
            {
                PushError(result, "已存在相同名称的媒体");
            }
        }
    }
}
